﻿
// template
function ng_cppei_pbarRegdirective(app) {

    app.directive("pages", function () {
        return {
            restrict: 'E',
            template: '<div class="modal-footer no-margin-top">'
                + '<div class="pull-left">从{{(currPageIndex)*15+currCount}}到{{(currPageIndex)*15+currCount}}/共{{totalItem}}条</div>'
                +'<div class="pull-right">'
                   +'<ul class="pagination pull-right no-margin">'
                        + '<li ng-class="currPageIndex==0?\'prev disabled\':\'\'">'
                           +'<a ng-click="onPaging(0)" href="javascript:void(0);">'
                                +'<i class="icon-step-backward middle"></i></a></li>'
                        + ' <li ng-class="currPageIndex==0?\'prev disabled\':\'\'">'
                             +'<a ng-click="onPaging(currPageIndex-1)" href="javascript:void(0);">'
                                +' <i class="icon-caret-left middle"></i>'
                             +'</a></li>'
                         + '<li ng-class="page.isSelected?\'active\':\'\'" ng-repeat="page in PageInfos">'
                            +' <a ng-click="onPaging(page.value)" href="javascript:void(0);">{{page.text}}</a></li>'
                        + ' <li ng-class="currPageIndex==maxPageIndex?\'prev disabled\':\'\'">'
                             +'<a ng-click="onPaging(currPageIndex+1)" href="javascript:void(0);">'
                                 +'<i class="icon-caret-right middle"></i></a></li>'
                         + '<li ng-class="currPageIndex==maxPageIndex?\'prev disabled\':\'\'">'
                             +'<a ng-click="onPaging(maxPageIndex)" href="javascript:void(0);">'
                                 +'<i class="icon-step-forward middle"></i></a></li>'
                      +'</ul></div></div>',
            replace: true
    }
    });
}

function ng_cppei_pbarRegscope($scope) {
    $scope.maxPageIndex = 0;
    $scope.minPageIndex = 0;
    $scope.currPageIndex = 0;
    $scope.totalItem = 0;
    $scope.currCount = 0;
}

function ng_cppei_pbarpages(data,$scope){
    $scope.PageInfos = new Array();
    // debugger;
    // 计算可用的页码
    var maxPageIndex = parseInt(data.Data.Total / data.Data.Limit);
    if (data.Data.Total % data.Data.Limit != 0) {
        maxPageIndex++;
    }
    // 优化
    var minPageIndex = 0;
    for (var pageIndex = minPageIndex; pageIndex < maxPageIndex; pageIndex++) {
        var pageInfo = new PageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.Data.Page == pageIndex ? true : false);
        $scope.PageInfos.push(pageInfo);
    }

    $scope.totalItem = data.Data.Total;
    $scope.maxPageIndex = maxPageIndex - 1;
    $scope.minPageIndex = minPageIndex;
    $scope.currPageIndex = 0;
    $scope.currCount = data.Data.Count;
}

function ng_cppei_pbarIsSelected(pages,data,$scope){
    angular.forEach($scope.PageInfos, function (page, index) {
        page.isSelected = false;
        if (page.value == pages) {
            page.isSelected = true;
        }
    });

    $scope.currPageIndex = pages;
    $scope.currCount = data.Data.Count;

}

//
// @value:页码
// @text:文字
// @tooptip:
// @isSelected:是否选中（当前页）
function PageInfo(value, text, tooltip, isSelected) {
    this.value = value;
    this.text = text;
    this.tooltip = tooltip;
    this.isSelected = isSelected;
}